//
//  ASAchievementDetailVC.m
//  AttendenceSys
//
//  Created by 窦芳俊 on 16/3/29.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASAchievementDetailVC.h"
#import "ASAchiDetailCell.h"
#import "AchievementInfo.h"
#import "ASAddAchievementVC.h"
#import "ASShowAchievementVC.h"


@interface ASAchievementDetailVC ()

@end

@implementation ASAchievementDetailVC

- (instancetype)initWithAchiTypeId:(NSInteger)typeId typeName:(NSString *)typeName
{
    self = [super init];
    if (self)
    {
        _achiTypeId = typeId;
        _achiTypeName = typeName;
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSString *total = [NSString stringWithFormat:@"%@", _achiTypeName];
    self.strNavTitle = total;
    [self setRightText:@"增加"];
    [self initUI];
    
    //[self studentFetchAchievement];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [_achiArray removeAllObjects];
    [self studentFetchAchievement];
}

- (void)initUI
{
    // detailTableView
    _detailTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _detailTableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _detailTableView.delegate = self;
    _detailTableView.dataSource = self;
    _detailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _detailTableView.backgroundColor = [UIColor whiteColor];
    _detailTableView.rowHeight = kASAchiDetailCellHeight;
    [_detailTableView registerClass:[ASAchiDetailCell class] forCellReuseIdentifier:NSStringFromClass([ASAchiDetailCell class])];
    [self.view addSubview:_detailTableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)rightButtonPressed:(id)action
{
    NSString *total = [NSString stringWithFormat:@"%@", _achiTypeName];
    
    ASAddAchievementVC *adVC = [[ASAddAchievementVC alloc] initWithTypeId:_achiTypeId typeName:total];
    adVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:adVC animated:YES];
}

// 学生查看自己的状态
- (void)studentFetchAchievement
{
    //获取用户成果相关数据 http://121.42.185.124:8080/LawSchool/getUserCondition.action?userCode=01120603&conditonTypeId=1
    //-1表示全部，1专业实习，2志愿服务，3社会实践，4其他
    
    NSString* refer = @"getUserCondition.action?";
    NSString* prefix = kASHttpUrl(refer);
    NSString* userCode = [ASAppDelegate sharedAppDelegate].userInfo.userCode;
    NSString* achiTypeId = [NSString stringWithFormat:@"%ld", (long)_achiTypeId];
    NSString* totalUrl = [NSString stringWithFormat:@"%@userCode=%@&conditonTypeId=%@", prefix, userCode, achiTypeId];
    
    [SVProgressHUD showWithStatus:@"获取中..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    [networkingEngine GET:totalUrl parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            NSArray* data = [jsonRsp objectForKey:kASJsonData];
            
            [SVProgressHUD dismiss];
            
            [self parseData:data];
            [_detailTableView reloadData];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
    } failure:^(NSError *error) {
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

- (void)parseData:(NSArray*)arr
{
    if (_achiArray == nil)
    {
        _achiArray = [[NSMutableArray alloc] init];
    }
    
    for (int i = 0; i < [arr count]; i++)
    {
        AchievementInfo *info = [[AchievementInfo alloc] init];
        info.achiDate = [[arr objectAtIndex:i] objectForKey:@"conditionDate"];
        info.achiContent = [[arr objectAtIndex:i] objectForKey:@"conditionDesc"];
        info.achiLevel = [[arr objectAtIndex:i] objectForKey:@"conditionLevel"];
        info.achiPk = [[arr objectAtIndex:i] objectForKey:@"conditionPk"];
        info.achiTypeId = [[arr objectAtIndex:i] objectForKey:@"conditionTypeId"];
        info.achiUnit = [[arr objectAtIndex:i] objectForKey:@"conditionUnit"];
        info.userCode = [[arr objectAtIndex:i] objectForKey:@"userCode"];
        
        [_achiArray addObject:info];
    }
}

// 老师查看学生的状态
- (void)teacherFetchStudentAchievement
{
    //获取用户状况接口 http://121.42.185.124:8080/LawSchool/getStudentStatusList.action?userCode=xiaohu&teaCode=xiaohu conditionDesc状况的描述 2000汉字以内 teaCode老师的工号 userCode 要查看的学生的工号
    
    NSString* refer = @"getStudentStatusList.action?";
    NSString* prefix = kASHttpUrl(refer);
    NSString* teaCode = [ASAppDelegate sharedAppDelegate].userInfo.userCode;
    NSString* userCode = @"sjt";//
    NSString* totalUrl = [NSString stringWithFormat:@"%@userCode=%@&teaCode=%@", prefix, userCode, teaCode];
    
    [SVProgressHUD showWithStatus:@"获取中..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    [networkingEngine GET:totalUrl parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            NSDictionary* data = [jsonRsp objectForKey:kASJsonData];
            data = [data processDictionaryIsNSNull:data];
            
            [SVProgressHUD dismiss];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
    } failure:^(NSError *error) {
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_achiArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ASAchiDetailCell class])];
    ASAchiDetailCell *acCell = (ASAchiDetailCell *)cell;
    AchievementInfo *info = [_achiArray objectAtIndex:[indexPath row]];
    [acCell updateCellStatus:info];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    AchievementInfo *info = [_achiArray objectAtIndex:[indexPath row]];
    ASShowAchievementVC *showVC = [[ASShowAchievementVC alloc] initWithInfo:info];
    showVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:showVC animated:YES];
}

@end





